package cn.youth996.nav.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.youth996.nav.domain.GeoPoint;
import cn.youth996.nav.domain.GeoPointPic;
import cn.youth996.nav.dto.GeoPointDTO;
import cn.youth996.nav.mapper.GeoPointMapper;
import cn.youth996.nav.service.GeoPointPicService;
import cn.youth996.nav.service.GeoPointService;
import cn.youth996.nav.util.PageUtils;
import cn.youth996.nav.util.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 地理坐标信息
 *
 * @author Zhan Xinjian
 * @date 2025/7/10
 */
@Service
public class GeoPointServiceImpl extends ServiceImpl<GeoPointMapper, GeoPoint>
        implements GeoPointService {
    @Autowired
    private GeoPointPicService geoPointPicService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 构建查询条件包装器
        QueryWrapper<GeoPoint> wrapper = this.getWrapper(params);

        // 执行分页查询
        IPage<GeoPoint> page = this.page(
                new Query<GeoPoint>().getPage(params),
                wrapper
        );

        // 返回封装好的分页结果
        return new PageUtils(page);
    }


    @Override
    public List<GeoPoint> list(Map<String, Object> params) {
        // 构建查询条件包装器
        QueryWrapper<GeoPoint> wrapper = this.getWrapper(params);
        // 使用MyBatis-Plus提供的方法进行查询，并返回查询结果
        return baseMapper.selectList(wrapper);
    }

    @Override
    @Transactional
    public void saveWithPic(GeoPointDTO dto) {
        GeoPoint geoPoint = BeanUtil.copyProperties(dto, GeoPoint.class);
        baseMapper.insert(geoPoint);
        List<GeoPointPic> picList = dto.getImageBase64List();
        picList.forEach(pic -> {
            pic.setPointId(geoPoint.getId());
            geoPointPicService.save(pic);
        });
    }

    @Override
    public void updateWithPic(GeoPointDTO dto) {
        GeoPoint geoPoint = BeanUtil.copyProperties(dto, GeoPoint.class);
        baseMapper.updateById(geoPoint);
        //先删除图片，再增加
        geoPointPicService.remove(new QueryWrapper<GeoPointPic>().eq("point_id", geoPoint.getId()));
        List<GeoPointPic> picList = dto.getImageBase64List();
        picList.forEach(pic -> {
            pic.setPointId(geoPoint.getId());
            geoPointPicService.save(pic);
        });
    }

    public QueryWrapper<GeoPoint> getWrapper(Map<String, Object> params) {
        // 从参数映射表中获取设备代码
        String name = (String) params.get("name");
        // 创建一个QueryWrapper对象，用于构建查询条件
        QueryWrapper<GeoPoint> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), "name", name);
        return wrapper;
    }
}




